//
//  MarketingTwoCell.m
//  EMMApp
//
//  Created by Mac on 16/6/30.
//  Copyright © 2016年 YiXun. All rights reserved.
//

#import "MarketingTwoCell.h"
#import "HousingCVCell.h"
#import "MarketingThreeCell.h"
#import "FloorList.h"
#import "UnitList.h"

@interface MarketingTwoCell () {
    NSInteger num;
}
@property (nonatomic, retain) NSArray *floorList;
@property (nonatomic, strong) NSMutableArray *roomList;
@property (nonatomic, strong) UIScrollView  *scrollView;

@end

@implementation MarketingTwoCell

- (void)awakeFromNib {
    [super awakeFromNib];
//    _l1.backgroundColor = [UIColor colorFromHexCode:@"#e7f3f3"];
//    _l2.backgroundColor = [UIColor colorFromHexCode:@"#cde7e6"];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (void)initWithNSMutableArray:(NSMutableArray *)mArr indexNum:(NSInteger)indexNum {    
    _floorList = mArr;
    num = indexNum;
    
//    [self creatCollectionView];
/*
    _scrollView = [[UIScrollView  alloc] initWithFrame:CGRectMake(0, 41, EMM_WIDTH, _floorList.count*70)];
    [self addSubview:_scrollView];
    //这个属性很重要，它可以决定是横向还是纵向滚动，一般来说也是其中的 View 的总宽度，和总的高度
    //这里同时考虑到每个 View 间的空隙，所以宽度是 200x3＋5＋10＋10＋5＝630 emm-20/3*count
    //高度上与 ScrollView 相同，只在横向扩展，所以只要在横向上滚动
    float h = [_roomList[num] count]/3.00;
    _scrollView.contentSize = CGSizeMake(EMM_WIDTH*3, 70);
    
    //用它指定 ScrollView 中内容的当前位置，即相对于 ScrollView 的左上顶点的偏移
    _scrollView.contentOffset = CGPointMake(0, 0);
    //按页滚动，总是一次一个宽度，或一个高度单位的滚动
    //滑动时是否显示滑动条
    _scrollView.showsHorizontalScrollIndicator = YES;
    _scrollView.pagingEnabled =  YES;*/
    
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, _floorList.count*50.f)];
    scrollView.userInteractionEnabled = YES;
    
    [self addSubview:scrollView];
    
    NSInteger colCount = 0;
    for (NSInteger i=0; i<_floorList.count; i++) {
        for (NSInteger j=0; j<[_floorList[i] count]; j++) {
            if (j > colCount) {
                colCount = j;
            }
            
            RoomList *list = _floorList[i][j];
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(15+(100+15)*j, 10+(35+15)*i, 100, 35);
            [button setTitle:list.roomName forState:UIControlStateNormal];
            [scrollView addSubview:button];
            button.tag = 1000*i + j;
            button.layer.masksToBounds = YES;
            button.layer.cornerRadius = 4;
            button.titleLabel.font = [UIFont systemFontOfSize:14];
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            if ([list.roomStatus isEqualToString:@"销控"]) {
                button.backgroundColor = [UIColor colorFromHexCode:@"#d3dbe7"];
            }else if ([list.roomStatus isEqualToString:@"可售"]) {
                //button.backgroundColor = [UIColor colorFromHexCode:@"#6ac96b"];
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                button.backgroundColor = [UIColor whiteColor];
                button.layer.borderColor = [[UIColor colorFromHexCode:@"#CCCCCC"]CGColor];
                button.layer.borderWidth = 0.5f;
                button.layer.cornerRadius = 4.f;
                button.layer.masksToBounds = YES;
            }else if ([list.roomStatus isEqualToString:@"认筹"]) {
                button.backgroundColor = [UIColor colorFromHexCode:@"#00ff00"];
            }else if ([list.roomStatus isEqualToString:@"认购"]) {
                button.backgroundColor = [UIColor colorFromHexCode:@"#ff0000"];
            }else if ([list.roomStatus isEqualToString:@"签约"]) {
                button.backgroundColor = [UIColor colorFromHexCode:@"#cd00ff"];
            }else if ([list.roomStatus isEqualToString:@"保留"]) {
                button.backgroundColor = [UIColor colorFromHexCode:@"#66cdcc"];
            }else if ([list.roomStatus isEqualToString:@"暂不可售"]) {
                button.backgroundColor = [UIColor colorFromHexCode:@"#818181"];
            }
        }
    }
     scrollView.contentSize = CGSizeMake((160)*colCount, _floorList.count*50);
}

- (void)buttonClick:(UIButton *)btn {
//    if (self.delegate!=nil &&[self.delegate respondsToSelector:@selector(marketingTwoCelldidSelectedTableview:withcollecttion:)]) {
////        [self.delegate marketingTwoCelldidSelectedTableview:num withcollecttion:indexPath];
//    }
    
    NSInteger i = btn.tag/1000;
    NSInteger j = btn.tag%1000;
    RoomList *list = _floorList[i][j];
    if (_marketingBlock) {
        self.marketingBlock(list.roomId);
    }
}



#pragma mark - ====
#pragma mark - UICollectionView初始化
- (void)creatCollectionView {
    
    UICollectionViewFlowLayout *layuet = [[UICollectionViewFlowLayout alloc] init];
    //垂直行间距
    layuet.minimumLineSpacing = 1;
    //水平间隔
    layuet.minimumInteritemSpacing = 1;
    [layuet setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    self.homeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, EMM_WIDTH, 70*_floorList.count) collectionViewLayout:layuet];
    [self.homeCollectionView registerNib:[UINib nibWithNibName:@"HousingCVCell" bundle:nil] forCellWithReuseIdentifier:@"HousingCVCell"];
    self.homeCollectionView.delegate = self;
    self.homeCollectionView.dataSource = self;
    self.homeCollectionView.backgroundColor = [UIColor colorFromHexCode:@"f6fafb"];
    self.homeCollectionView.showsVerticalScrollIndicator = NO;
    self.homeCollectionView.showsHorizontalScrollIndicator = NO;
    [self addSubview:self.homeCollectionView];
}

#pragma mark - UICollectionViewDataSource / UICollectionViewDelegate /UICollectionViewDelegateFlowLayout 代理
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
//    return 1;
    return _floorList.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    return _floorList.count;
    return [_floorList[section] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"HousingCVCell";
    HousingCVCell *homeCell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    homeCell.backgroundColor = [UIColor colorFromHexCode:@"f6fafb"];
    homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#999999"];

//    RoomList *list = [_floorList objectAtIndex:indexPath.row];
    RoomList *list = _floorList[indexPath.section][indexPath.row];

    
    homeCell.titleLabel.layer.masksToBounds = YES;
    homeCell.titleLabel.layer.cornerRadius = 4;
    homeCell.titleLabel.text = list.roomName;
    if ([list.roomStatus isEqualToString:@"销控"]) {
        homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#999999"];
    }else if ([list.roomStatus isEqualToString:@"可售"]) {
        homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#6ac96b"];
    }else if ([list.roomStatus isEqualToString:@"小订"]) {
        homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#ff6600"];
    }else if ([list.roomStatus isEqualToString:@"认购"]) {
        homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#ffcc00"];
    }else if ([list.roomStatus isEqualToString:@"签约"]) {
        homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#f33e47"];
    }else if ([list.roomStatus isEqualToString:@"保留"]) {
        homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#66cdcc"];
    }else if ([list.roomStatus isEqualToString:@"暂不可售"]) {
        homeCell.titleLabel.backgroundColor = [UIColor colorFromHexCode:@"#3b3b3b"];
    }
    return homeCell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((EMM_WIDTH-20)/3, 60);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//    RoomList *list = [_roomList objectAtIndex:indexPath.row];
//    HousingDetailsViewController *evc = [[HousingDetailsViewController alloc] init];
//    evc.roomId = list.roomId;
//    [self.navigationController pushViewController:evc animated:YES];
    if (self.delegate!=nil &&[self.delegate respondsToSelector:@selector(marketingTwoCelldidSelectedTableview:withcollecttion:)]) {
        [self.delegate marketingTwoCelldidSelectedTableview:num withcollecttion:indexPath];
    }
}

@end
